my $mc = 'A';
my $linecntr = 0;
my $file = shift;
my $pnxt = 0;
my ($stop,$start);
my $cycle=0;
my $correction=shift;

open KOKOS, "<$file" or die "File $file cannot be open!\n";

binmode KOKOS;

undef($stop);
while(1) {
	read(KOKOS, $mc, 1) or exit 0;
	$linecntr = tell(KOKOS);

	if( $pnxt == 6 ) {
		if( defined($stop)) {
			#printf("%8.8x: endstream offset\n", $linecntr-10 );
	   	printf("%d: endstream offset\n", $linecntr-10 );
			#printf("%8.8x: len\n", $linecntr-10-$start );
	   	printf("%d: len\n", $linecntr-10-$start );
			$stop=$linecntr-10-$start;

			$cmd=sprintf("dd if=%s bs=1 skip=%d count=%d of=obj.%d.flatedecode\n", $file, ($start+$correction), ($stop-$correction), $cycle );
			print STDERR $cmd; 
			$cmd=sprintf("perl decoder.pl obj.%d.flatedecode obj.%d.plain\n", $cycle, $cycle );
			print STDERR $cmd; 

			$pnxt = 0;
			undef($stop);
			$cycle++;
		} else {
			#printf("%8.8x: stream offset\n", $linecntr );
	   	printf("%d: stream offset\n", $linecntr );
			$start=$linecntr;
			$pnxt = 0;
			$stop=1;
		}
		next;
	}

	if( ord($mc) == ord('s') ){
		$pnxt = 1;
		next;
	}

	if( ord($mc) == ord('t') && ($pnxt == 1)) {
		$pnxt++;
		next;
	}

	if( ord($mc) == ord('r') && ($pnxt == 2)) {
		$pnxt++;
		next;
	}

	if( ord($mc) == ord('e') && ($pnxt == 3)) {
		$pnxt++;
		next;
	}

	if( ord($mc) == ord('a') && ($pnxt == 4)) {
		$pnxt++;
		next;
	}

	if( ord($mc) == ord('m') && ($pnxt == 5)) {
		$pnxt++;
		next;
	}
	$pnxt=0;

#	seek(KOKOS,((ord($lh)*256 + ord($ll))-4),1);
}
close KOKOS;

=cut

0xFF 0xC0 (SOF0 identifier)
length (high byte, low byte), 8+components*3
data precision (1 byte) in bits/sample, usually 8 (12 and 16 not supported by most software)
image height (2 bytes, Hi-Lo), must be >0 if DNL not supported
image width (2 bytes, Hi-Lo), must be >0 if DNL not supported
number of components (1 byte), usually 1 = grey scaled, 3 = color YCbCr or YIQ, 4 = color CMYK)
for each component: 3 bytes
component id (1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q)
sampling factors (bit 0-3 vert., 4-7 hor.)
quantization table number
